package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 星河一叶Ck
 * @date 2024/10/6
 */

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址业务接口
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址业务接口")
    public Result<String> addAddress(@RequestBody AddressBook addressBook) {
        log.info("地址簿新增地址:{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询所有地址业务接口
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有地址业务接口")
    public Result<List<AddressBook>> showAddressBook() {
        return Result.success(addressBookService.list());
    }

    /**
     * 设置默认地址业务接口
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址业务接口")
    public Result<String> setDefaultAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id) {
        return Result.success(addressBookService.getAddressBookById(id));
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 查询默认地址: 用户下单的时候可以捕获一个默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefaultAddressBook() {
        return Result.success(addressBookService.getDefaultAddressBook());
    }

    /**
     * 根据id删除地址
     * @param id 查询参数方式查询 所以直接用对应的形参名进行接收
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> deleteAddressBookById(Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }
}
